package com.ruoyi.points.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.points.domain.PointsItem;
import com.ruoyi.points.service.IPointsItemService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 优惠券页面Controller
 *
 * @author ruoyi
 * @date 2024-08-01
 */
@RestController
@RequestMapping("/item")
public class PointsItemController extends BaseController
{
    @Autowired
    private IPointsItemService pointsItemService;

    /**
     * 查询优惠券页面列表
     */
    @RequiresPermissions("points:item:list")
    @GetMapping("/list")
    public TableDataInfo list(PointsItem pointsItem)
    {
        startPage();
        List<PointsItem> list = pointsItemService.selectPointsItemList(pointsItem);
        return getDataTable(list);
    }

    /**
     * 导出优惠券页面列表
     */
    @RequiresPermissions("points:item:export")
    @Log(title = "优惠券页面", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointsItem pointsItem)
    {
        List<PointsItem> list = pointsItemService.selectPointsItemList(pointsItem);
        ExcelUtil<PointsItem> util = new ExcelUtil<PointsItem>(PointsItem.class);
        util.exportExcel(response, list, "优惠券页面数据");
    }

    /**
     * 获取优惠券页面详细信息
     */
    @RequiresPermissions("points:item:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
//        通过id查找对象
        PointsItem data = pointsItemService.selectPointsItemById(id);
//        判断如果该value不为空
        if(StringUtils.isNotEmpty(data.getPointsValue())){
//            定义一个变量获取这个value
            String pointsValue = data.getPointsValue();
//            把value的括号去掉
//            通过逗号分割value
            String[] split = pointsValue.split(",");
            ArrayList<Object > objects = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                objects.add(split[i]);
            }
            data.setPointsValueList(objects);
            System.out.println("分割后的数组"+objects);
        }
        return success(data);
    }

    /**
     * 新增优惠券页面
     */
    @RequiresPermissions("points:item:add")
    @Log(title = "优惠券页面", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointsItem pointsItem)
    {
        if(pointsItem.getPointsValueList()!=null&&!pointsItem.getPointsValueList().isEmpty()){
            ArrayList<Object> pointsValueList = pointsItem.getPointsValueList();
            String str="";
            for (int i = 0; i < pointsValueList.size(); i++) {
                if(pointsValueList.size()-1==i){
                    str+=pointsValueList.get(i);
                }else {
//                数组的最后一个元素
                    str+=pointsValueList.get(i)+",";
                }
            }
            String result = str.replaceAll("\\[|\\]", "");
            pointsItem.setPointsValue(result);
        }
        return toAjax(pointsItemService.insertPointsItem(pointsItem));
    }

    /**
     * 修改优惠券页面
     */
    @RequiresPermissions("points:item:edit")
    @Log(title = "优惠券页面", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointsItem pointsItem)
    {
        if(pointsItem.getPointsValueList()!=null&&!pointsItem.getPointsValueList().isEmpty()){
            ArrayList<Object> pointsValueList = pointsItem.getPointsValueList();
            String str="";
            for (int i = 0; i < pointsValueList.size(); i++) {
                if(pointsValueList.size()-1==i){
                    str+=pointsValueList.get(i);
                }else {
//                数组的最后一个元素
                    str+=pointsValueList.get(i)+",";
                }
            }
            String result = str.replaceAll("\\[|\\]", "");
            pointsItem.setPointsValue(result);
        }
        return toAjax(pointsItemService.updatePointsItem(pointsItem));
    }

    /**
     * 删除优惠券页面
     */
    @RequiresPermissions("points:item:remove")
    @Log(title = "优惠券页面", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointsItemService.deletePointsItemByIds(ids));
    }




}
